<template>

<div class="bruce">
    <ul class="bubble-bgwall">
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
    </ul>
  </div>


    <div class="register-container">
        <div class="register-box">
            <div class="register-header">
                <h1>注册新用户</h1>
            </div>

            <form @submit.prevent="handleRegister" class="register-form">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input id="username" v-model="username" type="text" placeholder="请输入用户名" required />
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <div class="password-container">
                        <input id="password" v-model="password" :type="showPassword ? 'text' : 'password'"
                            placeholder="请输入密码" required />
                        <!-- <button type="button" class="eye-button" @mousedown="showPassword = true"
                            @mouseup="showPassword = false" @mouseleave="showPassword = false"
                            @touchstart="showPassword = true" @touchend="showPassword = false">
                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"
                                fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                stroke-linejoin="round">
                                <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                                <circle cx="12" cy="12" r="3"></circle>
                            </svg>
                        </button> -->
                    </div>
                </div>

                <div class="form-group">
                    <label for="confirmPassword">请再次输入密码</label>
                    <div class="password-container">
                        <input id="confirmPassword" v-model="confirmPassword"
                            :type="showConfirmPassword ? 'text' : 'password'" placeholder="请再次输入密码" required />
                        <!-- <button type="button" class="eye-button" @mousedown="showConfirmPassword = true"
                            @mouseup="showConfirmPassword = false" @mouseleave="showConfirmPassword = false"
                            @touchstart="showConfirmPassword = true" @touchend="showConfirmPassword = false">
                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"
                                fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                stroke-linejoin="round">
                                <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                                <circle cx="12" cy="12" r="3"></circle>
                            </svg>
                        </button> -->
                    </div>
                </div>

                <div class="button-container">
                    <button type="submit" class="register-button">注册</button>
                </div>

                <div v-if="error" class="error-message">
                    {{ error }}
                </div>
            </form>
        </div>

        <!-- 成功弹窗 -->
        <div v-if="showSuccessModal" class="success-modal" @click="closeModal">
            <div class="modal-content">
                <p>注册成功！</p>
            </div>
        </div>
    </div>
</template>

<script>
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import axios from 'axios';

axios.defaults.baseURL = 'http://localhost:3000';

export default {
    setup() {
        const store = useStore();
        const router = useRouter();

        const username = ref('');
        const password = ref('');
        const confirmPassword = ref('');
        const error = ref('');
        const showPassword = ref(false);
        const showConfirmPassword = ref(false);
        const showSuccessModal = ref(false);

        const handleRegister = async () => {
            if (password.value !== confirmPassword.value) {
                error.value = '两次输入的密码不一致';
                return;
            }

            try {
                const response = await axios.post('/register', {
                    username: username.value,
                    password: password.value
                });

                console.log('Register response:', response.data);

                // 注册成功后显示弹窗
                showSuccessModal.value = true;
            } catch (err) {
                console.error('Register error:', err);
                error.value = err.response?.data?.message || '注册失败，请检查用户名和密码';
            }
        };

        const closeModal = () => {
            showSuccessModal.value = false;
            router.push({ name: 'Login' });
        };

        return {
            username,
            password,
            confirmPassword,
            error,
            showPassword,
            showConfirmPassword,
            showSuccessModal,
            handleRegister,
            closeModal
        };
    }
};
</script>

<style scoped>
/* 测试隐藏小眼睛 */
input[type="password"]::-webkit-credentials-auto-fill-button,
input[type="password"]::-webkit-textfield-decoration-container {
    display: block;
}

/* 全局样式 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    height: 100vh;
    overflow: hidden;
    /* 防止滚动条出现 */
}

/* 注册容器 - 修改为右下角定位 */
.register-container {
    position: fixed;
    /* 使用固定定位 */
    right: 500px;
    /* 距离右边缘30px */
    bottom: 300px;
    /* 距离底部30px */
    width: 400px;
    /* 固定宽度 */
    z-index: 1000;
    /* 确保在页面最上层 */
}

/* 注册框 */
.register-box {
    background: white;
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    width: 100%;
    max-width: 400px;
    overflow: hidden;
    animation: fadeIn 0.5s ease;
}

/* 注册头部 */
.register-header {
    padding: 30px;
    text-align: center;
    background: #a373ca;
    color: white;
}

.logo {
    width: 60px;
    height: 60px;
    margin-bottom: 15px;
}

.register-header h1 {
    font-size: 24px;
    font-weight: 600;
}

/* 注册表单 */
.register-form {
    padding: 30px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: #333;
}

.form-group input {
    width: 100%;
    padding: 12px 15px;
    border: 1px solid #ddd;
    border-radius: 5px;
    font-size: 16px;
    transition: border-color 0.3s;
}

.form-group input:focus {
    border-color: #2475d1;
    outline: none;
}

/* 密码输入框和眼睛图标 */
.password-container {
    position: relative;
}


.eye-button {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    cursor: pointer;
    color: #666;
    padding: 0;
}

.eye-button:hover {
    color: #2475d1;
}

/* 按钮容器 */
.button-container {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

/* 注册按钮 */
.register-button {
    flex: 1;
    padding: 12px;
    background: #be9bec;
    color: rgb(96, 63, 216);
    border: none;
    border-radius: 5px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    border: 1px solid #2c9db1;
}

.register-button:hover {
    background: #1a7b8c;
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(44, 157, 177, 0.3);
}

/* 错误消息 */
.error-message {
    margin-top: 20px;
    padding: 10px;
    background: #ffe6e6;
    color: #d32f2f;
    border-radius: 5px;
    text-align: center;
}

/* 成功弹窗 */
.success-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    cursor: pointer;
}

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    animation: scaleIn 0.3s ease;
}

.modal-content p {
    font-size: 24px;
    color: #4CAF50;
    margin-bottom: 20px;
}

.modal-content button {
    padding: 10px 20px;
    background: #4CAF50;
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.3s;
}

.modal-content button:hover {
    background: #388E3C;
}






/* 气泡背景样式 */
html, body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}

.bruce {
  height: 100%;
  background-image: linear-gradient(270deg, #8146b4, #6990f6);
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: -1;
}

.bubble-bgwall {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  animation: bubble 15s infinite;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 160px;
  height: 160px;
  animation-delay: 5s;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }
  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }
  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }
  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}
</style>
